package m2;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * 信息消费者
 * @author 郭鲁政
 */
public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.64.140");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        Channel channel = connectionFactory.newConnection().createChannel();
        DeliverCallback deliverCallback = (String consumerTag, Delivery message)->{
            byte[] body = message.getBody();
            String s = new String(body);
            System.out.println("收到消息"+s);
            for (char c : s.toCharArray()) {
                if (c=='.'){
                    System.out.println("线程休眠中");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        };
        CancelCallback cancelCallback =(String consumerTag) -> {


        };
        channel.basicConsume("test-1", false,deliverCallback,cancelCallback);
    }
}
